package GraphType.shortestPath;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;
import java.util.Scanner;

public class BFS {

    static int p, q, startX, startY, min = 99999;
    static int[][] a = new int[10][10];//1表示空地，2表示障碍物
    static int v[][] = new int[10][10];//0表示未访问，1表示已访问
    static int dx[] = {0, 1, 0, -1};
    static int dy[] = {1, 0, -1, 0};
    static Deque<point> r = new ArrayDeque<>();

    private static class point {
        int x;
        int y;
        int step;

        public point() {
        }

        public point(int x, int y, int step) {
            this.x = x;
            this.y = y;
            this.step = step;
        }

        public point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return "point{" +
                    "x=" + x +
                    ", y=" + y +
                    ", step=" + step +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            point point = (point) o;
            return x == point.x &&
                    y == point.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y, step);
        }
    }

    public static void main(String[] args) {
        bfs();
    }

    private static void bfs() {
        init();
        point start = new point(startX, startY, 0);
        point end = new point(p, q);
        r.add(start);//起点入队
        v[startX][startY] = 1;
        boolean hasFound = false;
        while (!r.isEmpty()) {
            point first = r.getFirst();
            if (first.equals(end)) {
                hasFound = true;
                System.out.println(r.getFirst().step);
                break;
            }
            for (int i = 0; i < 4; i++) {
                int tx = first.x + dx[i];
                int ty = first.y + dy[i];
                if (a[tx][ty] == 1 && v[tx][ty] == 0) {
                    //入队拓展点
                    r.add(new point(tx, ty, first.step + 1));
                    v[tx][ty] = 1;
                }
            }
            r.pop();//拓展完后将队首出队
        }
        if (!hasFound) {
            System.out.println("no answer");
        }
    }


    public static void init() {
        //输入
        Scanner scanner = new Scanner(System.in);
        String[] arr = scanner.nextLine().split(" ");
        int m = Integer.parseInt(arr[0]);
        int n = Integer.parseInt(arr[1]);
        for (int i = 1; i <= m; i++) {
            String[] split = scanner.nextLine().split(" ");
            for (int j = 1; j <= n; j++) {
                a[i][j] = Integer.parseInt(split[j - 1]);
            }
        }
        //起点坐标，终点坐标
        String[] loc = scanner.nextLine().split(" ");
        startX = Integer.parseInt(loc[0]);
        startY = Integer.parseInt(loc[1]);
        p = Integer.parseInt(loc[2]);
        q = Integer.parseInt(loc[3]);
        System.out.printf("起点：(%d,%d)", startX, startY);
        System.out.printf("终点：(%d,%d)", p, q);
    }

}
